<?php
namespace Home\Controller;
use Think\Controller;

class CommonController extends Controller {
    protected $categories;
    protected $Category;

    public function __construct()
    {
        parent::__construct();
        $this->Category = M('Category');
        $this->categories = $this->Category->where('parent_id=0')->order('sort_order asc,id asc')->select();
        $this->children($this->categories);
        $this->assign("categories",$this->categories);
        //p($this->categories);
        //die;
    }

    private function children(&$category){

        foreach ($category as $key=>&$value){
            $value['child']=$this->Category->where("parent_id='$value[id]'")->select();
            $this->make_url($value);
            $this->children($value['child']);
        }
    }

    private function make_url(&$category){
        switch ($category['type']){
            case '1':
                $category['link'] = U('category',array('id'=>$category['id']));
                break;
            case '2':
                $category['link'] = U('lst',array('id'=>$category['id']));
                break;
            case '3':
                $category['link'] = $category['url'];
        }
    }
}